package com.example.notification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private NotificationManager manage;
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         manage = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

         if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.O){//版本判断，如果在8.0及8.0之上在进行创建
             NotificationChannel channel = new NotificationChannel("leo", "测试通知",
                     NotificationManager.IMPORTANCE_HIGH);
             manage.createNotificationChannel(channel);
         }

        Intent intent = new Intent(this, NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        notification = new NotificationCompat.Builder(this,"leo")//通知有标题，内容，小图标就可以发出通知，其他的可选写
                .setContentTitle("官方通知")//通知标题（必写）
                .setContentText("你好世界")//通知内容（必写）
                .setSmallIcon(R.drawable.ic_baseline_account_box_24)//设置小图标（必写）（图片不能带颜色--可用矢量图）
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ceshi))//甚至大图标（可以有颜色）
                .setColor(Color.parseColor("#ff0000")) //设置小图标的颜色
                .setContentIntent(pendingIntent) //设置点击通知后跳转意图
                .setAutoCancel(true)//设置点击之后自动清除通知
                .build();


    }

    public void sendNotification(View view) {
         manage.notify(1,notification);
    }

    public void cacelNotification(View view) {
        manage.cancel(1);
    }
}